﻿using System;
using System.IO;

namespace MediaLibraryManager
{
	public class MediaFileSystemWatcher : FileSystemWatcher
	{
		private MediaFileSystemEventHandler onMediaCreated;
		private MediaRenamedEventHandler onMediaRenamed;
		private MediaFileSystemEventHandler onMediaDeleted;

		public MediaType MediaType { get; set; }

		public MediaFileSystemWatcher()
			: base()
		{
			this.Created += new FileSystemEventHandler(fileSystemWatcher_Created);
			this.Renamed += new RenamedEventHandler(fileSystemWatcher_Renamed);
			this.Deleted += new FileSystemEventHandler(fileSystemWatcher_Deleted);
		}

		#region Created

		public event MediaFileSystemEventHandler MediaCreated
		{
			add { onMediaCreated += value; }
			remove { onMediaCreated -= value; }
		}

		private void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
		{
			OnMediaCreated(e.ChangeType, e.FullPath.Substring(0,e.FullPath.LastIndexOf('\\')), e.Name);
		}

		public void OnMediaCreated(WatcherChangeTypes changeType, string directory, string name)
		{
			if (this.onMediaCreated != null)
				onMediaCreated(this, new MediaFileSystemEventArgs(changeType, directory, name, this.MediaType));
		}

		#endregion

		#region Renamed

		public event MediaRenamedEventHandler MediaRenamed
		{
			add { onMediaRenamed += value; }
			remove { onMediaRenamed -= value; }
		}

		private void fileSystemWatcher_Renamed(object sender, RenamedEventArgs e)
		{
			OnMediaRenamed(e.ChangeType, e.FullPath.Substring(0,e.FullPath.LastIndexOf('\\')), e.Name,e.OldName);
		}

		public void OnMediaRenamed(WatcherChangeTypes changeType, string directory, string name, string oldName)
		{
			if (this.onMediaRenamed != null)
				onMediaRenamed(this, new MediaRenamedEventArgs(changeType, directory, name, oldName, this.MediaType));
		}

		#endregion

		#region Deleted

		public event MediaFileSystemEventHandler MediaDeleted
		{
			add { onMediaDeleted += value; }
			remove { onMediaDeleted -= value; }
		}

		private void fileSystemWatcher_Deleted(object sender, FileSystemEventArgs e)
		{
			OnMediaDeleted(e.ChangeType, e.FullPath.Substring(0,e.FullPath.LastIndexOf('\\')), e.Name);
		}

		public void OnMediaDeleted(WatcherChangeTypes changeType, string directory, string name)
		{
			if (this.onMediaDeleted != null)
				onMediaDeleted(this, new MediaFileSystemEventArgs(changeType, directory, name, this.MediaType));
		}

		#endregion
	}

	public delegate void MediaFileSystemEventHandler(object sender, MediaFileSystemEventArgs e);

	public class MediaFileSystemEventArgs : System.IO.FileSystemEventArgs
	{
		private MediaType mediaType;

		public MediaFileSystemEventArgs(WatcherChangeTypes changeType, string directory, string name, MediaType mediaType)
			: base(changeType, directory, name)
		{
			this.mediaType = mediaType;
		}

		public MediaType MediaType
		{
			get { return this.mediaType; }
		}
	}

	public delegate void MediaRenamedEventHandler(object sender, MediaRenamedEventArgs e);

	public class MediaRenamedEventArgs : System.IO.RenamedEventArgs
	{
		private MediaType mediaType;

		public MediaRenamedEventArgs(WatcherChangeTypes changeType, string directory, string name, string oldName, MediaType mediaType)
			: base(changeType, directory, name, oldName)
		{
			this.mediaType = mediaType;
		}

		public MediaType MediaType
		{
			get { return this.mediaType; }
		}
	}
}